//
//  NotiViewController.m
//  ObjcGoNew
//
//  Created by 邢伟新 on 2021/4/28.
//

#import "NotiViewController.h"
#import <Masonry/Masonry.h>

@interface NotiViewController ()

@end

@implementation NotiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"通知";
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *barrierButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [barrierButton setTitle:@"发送通知" forState:(UIControlStateNormal)];
    [barrierButton addTarget:self action:@selector(sendNoti) forControlEvents:(UIControlEventTouchUpInside)];
    barrierButton.backgroundColor =[UIColor purpleColor];
    barrierButton.tag = 1001;
    [self.view addSubview:barrierButton];
    [barrierButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(50);
        make.top.mas_equalTo(150);
    }];
    
    [self addObserver];
}

-(void)sendNoti{
    
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    dispatch_async(queue, ^{
        NSLog(@"%@", [NSString stringWithFormat:@"--1---%@",[NSThread currentThread]]);
        NSDictionary *dic = @{@"key":@"value"};
        [[NSNotificationCenter defaultCenter]postNotificationName:@"xingweixin" object:self userInfo:dic];
        
    });
    
   
    
}


-(void)addObserver{
    
    NSLog(@"%@", [NSString stringWithFormat:@"--2---%@",[NSThread currentThread]]);
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(notAction:) name:@"xingweixin" object:nil];
}
-(void)notAction:(NSNotification *)not{
    NSLog(@"%@", [NSString stringWithFormat:@"--3---%@",[NSThread currentThread]]);
    NSLog(@"--收到了通知---%@",not.userInfo);
    //多线程中发送的通知,都可以接收到,但是他是和发送通知是在同一线程
    dispatch_async(dispatch_get_main_queue(), ^{
        //处理UI
    });
    
}

-(void)dealloc{
    //通知移除
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"xingweixin" object:self];
}

@end
